/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shelvesreport.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.shelvesreport.entity.ShelvesReport;
import com.thinkgem.jeesite.modules.shelvesreport.service.ShelvesReportService;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 上架报告Controller
 * @author zxl
 * @version 2017-08-04
 */
@Controller
@RequestMapping(value = "${adminPath}/shelvesreport/shelvesReport")
public class ShelvesReportController extends BaseController {

	@Autowired
	private ShelvesReportService shelvesReportService;
	@Autowired
	private OfficeService officeService;

	@ModelAttribute
	public ShelvesReport get(@RequestParam(required=false) String id) {
		ShelvesReport entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = shelvesReportService.get(id);
		}
		if (entity == null){
			entity = new ShelvesReport();
		}
		return entity;
	}
	
	@RequiresPermissions("shelvesreport:shelvesReport:view")
	@RequestMapping(value = {"list", ""})
	public String list(ShelvesReport shelvesReport, HttpServletRequest request, HttpServletResponse response, Model model) {

		//getWar(shelvesReport);

		Page<ShelvesReport> page = shelvesReportService.findPage(new Page<ShelvesReport>(request, response), shelvesReport);
		//shelvesReport.setWarhoseName("");
		model.addAttribute("page", page);
		return "modules/shelvesreport/shelvesReportList";
	}

	@RequiresPermissions("shelvesreport:shelvesReport:view")
	@RequestMapping(value = "form")
	public String form(ShelvesReport shelvesReport, Model model) {
		model.addAttribute("shelvesReport", shelvesReport);
		return "modules/shelvesreport/shelvesReportForm";
	}

	@RequiresPermissions("shelvesreport:shelvesReport:edit")
	@RequestMapping(value = "save")
	public String save(ShelvesReport shelvesReport, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, shelvesReport)){
			return form(shelvesReport, model);
		}
		shelvesReportService.save(shelvesReport);
		addMessage(redirectAttributes, "保存上架报告成功");
		return "redirect:"+Global.getAdminPath()+"/shelvesreport/shelvesReport/?repage";
	}
	
	@RequiresPermissions("shelvesreport:shelvesReport:edit")
	@RequestMapping(value = "delete")
	public String delete(ShelvesReport shelvesReport, RedirectAttributes redirectAttributes) {
		shelvesReportService.delete(shelvesReport);
		addMessage(redirectAttributes, "删除上架报告成功");
		return "redirect:"+Global.getAdminPath()+"/shelvesreport/shelvesReport/?repage";
	}
	/**
	 * 导出数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("shelvesreport:shelvesReport:view")
	@RequestMapping(value = "export", method= RequestMethod.POST)
	public String exportFile(ShelvesReport shelvesReport, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			/*getWar(shelvesReport);*/
			String fileName = "上架报告"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			Page<ShelvesReport>pageConfig = new Page<ShelvesReport>(request, response, -1);
			pageConfig.setPageSize(65535);
			Page<ShelvesReport> page = shelvesReportService.findPage(pageConfig, shelvesReport);
			new ExportExcel("上架报告", ShelvesReport.class).setDataList(page.getList()).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出收货！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/shelvesreport/shelvesReport/?repage";
	}

	private void getWar(ShelvesReport shelvesReport){
	/*	if(null ==shelvesReport || StringUtils.isEmpty(shelvesReport.getWarhoseName())){
			User user =  UserUtils.getUser();
			Office office = new Office();
			office.setParentIds(user.getOffice().getId());

			List<Office> all = officeService.findList(user.getOffice());
			List<String> warname = new ArrayList<String>();
			warname.add(user.getOffice().getName());
			for(Office one:all){
				warname.add(one.getName());
			}
			shelvesReport.setWarhoseName("'"+StringUtils.join(warname,"','")+"'");
		}else if(StringUtils.isNotEmpty(shelvesReport.getWarhoseName())){
			shelvesReport.setWarhoseName("'"+StringUtils.join(shelvesReport.getWarhoseName(),"','")+"'");
		}*/
	}
}